using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using System.Web.Configuration;
using System.Collections.Specialized;
using System.Data.Common;
using System.Text;
using System.Collections.Generic;
using System.Web.Caching;
using PigeonCms.Core.Helpers;

namespace PigeonCms
{
    /// <summary>
    /// static class to retrieve localized labels
    /// </summary>
    public static class LabelsProvider
    {
        const string CacheKeyPrefix = "Labels";

        #region public methods

        public static void ClearCacheByResourceSet(string resourceSet)
        {
            var cache = new CacheManager<List<ResLabel>>(CacheKeyPrefix);
            if (!cache.IsEmpty(resourceSet))
            {
                cache.Clear();
            }
        }

        public static List<ResLabel> GetLabelsByResourceSet(string resourceSet)
        {
            var result = new List<ResLabel>();

            var cache = new CacheManager<List<ResLabel>>(CacheKeyPrefix);
            if (cache.IsEmpty(resourceSet))
            {
                var man = new LabelsManager();
                var filter = new LabelsFilter();
                filter.ResourceSet = resourceSet;
                result = man.GetByFilter(filter, "");

                cache.Insert(resourceSet, result);
            }
            else
            {
                result = cache.GetValue(resourceSet);
            }
            return result;
        }

        public static string GetLocalizedLabelFromList(List<ResLabel>labelsList, string resourceId)
        {
            string res = "";

            if (labelsList != null)
            {
                //find the right value in labelsList
                try
                {
                    //try current culture
                    res = labelsList.Find(
                        delegate(ResLabel labelToFind)
                        {
                            if (labelToFind.ResourceId.ToLower() == resourceId.ToLower() &&
                                labelToFind.CultureName.ToLower() == Utility.GetCurrCultureName().ToLower())
                                return true;
                            else
                                return false;
                        }).Value;
                }
                catch (NullReferenceException)
                {
                    //try default culture
                    if (Config.CultureDefault.ToLower() != Utility.GetCurrCultureName().ToLower())
                    {
                        try
                        {
                            res = labelsList.Find(
                                delegate(ResLabel labelToFind)
                                {
                                    if (labelToFind.ResourceId.ToLower() == resourceId.ToLower() &&
                                        labelToFind.CultureName.ToLower() == Config.CultureDefault.ToLower())
                                        return true;
                                    else
                                        return false;
                                }).Value;
                        }
                        catch (NullReferenceException)
                        { res = ""; }
                    }
                }
            }
            return res;
        }

        public static string GetLocalizedVarFromList(List<ResLabel> labelsList, string varId)
        {
            string res = varId;
            if (varId.StartsWith("$"))
            {
                string resourceId = varId.Substring(1);
                res = GetLocalizedLabelFromList(labelsList, resourceId);
                if (string.IsNullOrEmpty(res))
                    res = varId;
            }
            return res;
        }

        public static void SetLocalizedControlVisibility(bool showOnlyDefaultCulture, string culture, Control ctrl)
        {
            if (showOnlyDefaultCulture)
            {
                if (culture.ToLower() != Config.CultureDefault.ToLower())
                    ctrl.Visible = false;
            }

        }

        #endregion
    }
}